package com.shao.im.controller;


import com.shao.commons.domain.Codes;
import com.shao.commons.domain.R;
import com.shao.commons.exception.ServiceException;
import com.shao.commons.utils.CodeUtils;
import com.shao.commons.utils.JwtUtils;
import com.shao.im.entity.User;
import com.shao.im.service.UserService;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户表(User)表控制层
 *
 * @author makejava
 * @since 2023-07-26 12:47:59
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Validated
public class UserController {

    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;

    /**
     * 注册
     * @param user
     * @return
     */
    @RequestMapping("/register")
    public R register(@Validated User user) {
        log.debug("[user register] 用户注册 - {}", user);
        userService.save(user);
        return R.success();
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public R login(@NotBlank(message = "用户名不能为空") String username, @NotBlank(message = "密码不能为空") String password) {
        //log.debug("[user login] 用户登录 - {} - {}", username, password);
        User user = userService.queryByUserName(username);

        if (user == null || !user.getPassword().equals(password)) {
            throw new ServiceException(Codes.LOGIN_FAIL);
        }
        // 登录成功则将用户信息转成jwt令牌
        String token = JwtUtils.createJwtToken()
                .add("uid", user.getId())
                .add("nickname", user.getNickname())
                .add("header", user.getHeader())
                .build();
        return R.success(token);
    }

    /**
     * 发送验证码
     * @param username
     * @return
     */
    @RequestMapping("/sendCode")
    public R sendCode(@NotBlank(message = "用户名不能为空") String username) {
        log.debug("[send code] 发送验证码找回密码 - {}", username);
        //发送邮件
        userService.sendEmailByUsername(username);
        return R.success();
    }

    /**
     * 通过邮箱修改密码
     * @param username 用户名
     * @param password 密码
     * @param code 验证码
     * @return 结果
     */
    @RequestMapping("/updatePassword")
    public R updatePassword(
            @NotBlank(message = "用户名不能为空") String username,
            @NotBlank(message = "密码不能为空") String password,
            @NotNull(message = "验证码不能为空") Integer code) {

        Integer sendCode = CodeUtils.getCode(username);
        if (sendCode == null || !sendCode.equals(code)) {
            // 验证失败
            throw new ServiceException(Codes.CODE_ERROR);
        }
        userService.update()
                .set("password", password)
                .eq("username", username)
                .update();
        return R.success();
    }

    /**
     * 根据关键字查询用户列表
     * @param keyword 关键字
     * @return 结果
     */
    @RequestMapping("/searchUsers")
    public R searchByKeyword(String keyword) {
        log.debug("[search users] 根据关键字查询用户列表 - {}", keyword);
        List<User> users = userService.search(keyword);
        log.debug("[users] 查询到的用户列表 - {}", users);
        return R.success(users);
    }
}

